const fs = require('fs');
const xlsx = require('node-xlsx');
// 工作目录，可存放多个 Excel 表格
var workPath = ""


fs.readdir(workPath, 'utf8', (err, data) => {
    data.forEach(item => {
        if (item.indexOf(".xlsx") > -1) {
            var pointTemplate = {
                "type": "FeatureCollection",
                "name": "",
                "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
                "features": []
            }
            var name = item.split(".")[0]
            var sheets = xlsx.parse(workPath + item);
            var safe = true
            var cnt = 0
            sheets.forEach(function (sheet) {
                var attrNames = sheet.data[0]
                var type = "unknown"
                var cursor = {
                    "LineString": 0,
                    "Polygon": 0,
                    "Point": {
                        "x": 0,
                        "y": 0,
                        "z": 0
                    }
                }
                attrNames.forEach((attr, index) => {
                    switch (attr) {
                        case "paths":
                            type = "LineString"
                            cursor[type] = index
                            break
                        case "rings":
                            type = "Polygon"
                            cursor[type] = index
                            break
                        case "x":
                            type = "Point"
                            cursor[type].x = index
                            break
                        case "y":
                            type = "Point"
                            cursor[type].y = index
                            break
                        case "z":
                            type = "Point"
                            cursor[type].z = index
                            break
                    }
                })
                if (type == "unknown") {
                    console.log("! " + name + "包含" + cnt + "个要素，数据地理几何信息缺失")
                } else {
                    for (var i = 1; i < sheet.data.length; i++) {
                        var f = { "type": "Feature", "properties": {}, "geometry": { "type": type, "coordinates": [] } }
                        sheet.data[i].forEach((item, index) => {
                            if (type == "Point") {
                                if (index !== cursor[type].x && index !== cursor[type].y && index !== cursor[type].z) {
                                    f.properties[attrNames[index]] = item
                                } else {
                                    f.geometry.coordinates = [Number(sheet.data[i][cursor[type].x] || 0), Number(sheet.data[i][cursor[type].y] || 0)]
                                }
                            } else {
                                if (index !== cursor[type]) {
                                    f.properties[attrNames[index]] = item
                                } else {
                                    item = item.split(",")
                                    var l = []
                                    for (var j = 0; j < item.length - 1; j += 2) {
                                        l.push([Number(item[j]), Number(item[j + 1])])
                                    }
                                    f.geometry.coordinates = l
                                }

                            }
                        })
                        pointTemplate.features.push(f)
                        cnt++
                    }
                    var str = JSON.stringify(pointTemplate, "", "\t")
                    fs.writeFile(workPath + name + '.json', str, function (err) {
                        if (err) { res.status(500).send('Server is error...') }
                    })
                    console.log(name + "包含" + cnt + "个要素，数据已生产完毕")
                }
            });

        }
    })
})
